﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Zsg_PeerToPeer
{
    public partial class FrmClient : Form
    {
        public FrmClient()
        {
            InitializeComponent();
        }
        PeerClient peerClient;
        private void Btn_Connection_Click(object sender, EventArgs e)
        {
            string hostname = Txt_Server.Text.Split(':')[0];
            int port = int.Parse(Txt_Server.Text.Split(':')[1]);
            peerClient = new PeerClient(hostname, port);
            peerClient.NoticeMsg += PeerClient_NoticeMsg;
            peerClient.ReceivedMsg += PeerClient_ReceivedMsg;
            peerClient.Init();
        }

        private void PeerClient_ReceivedMsg(object sender, string e)
        {
            PushMsg($"收到消息：{e}");
        }

        private void PeerClient_NoticeMsg(object sender, string e)
        {
            PushMsg(e);
        }

        private void Btn_Send_Click(object sender, EventArgs e)
        {
            peerClient.Send(Rtb_Msg.Text);
        }
        private void PushMsg(string Msg)
        {
            this.Invoke(new Action(() =>
            {
                Rtb_Msg.AppendText(Msg);
            }));
        }
    }
}
